package cst.neu.yuejian_31.model;

import java.util.List;

import cst.neu.yuejian_31.bean.MasterCommentBean;
import cst.neu.yuejian_31.iface.MasterCommentIface;
import cst.neu.yuejian_31.iface.MasterCommentListner;
import cst.neu.yuejian_31.Common.Constant;
import cst.neu.yuejian_31.service.MasterCommentService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by xxe on 2017/10/20.
 */

public class MasterCommentModel implements MasterCommentIface {
    private Retrofit retrofit;
    public MasterCommentModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Constant.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

    }


    @Override
    public void getmasterCommentList(String masterId, final MasterCommentListner mastercommentListner) {
        MasterCommentService mastercommentService=retrofit.create(MasterCommentService.class);
        Call<List<MasterCommentBean>>call=
                mastercommentService.getmasterCommentList(masterId);
        call.enqueue(new Callback<List<MasterCommentBean>>() {
            @Override
            public void onResponse(Call<List<MasterCommentBean>> call, Response<List<MasterCommentBean>> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    mastercommentListner.onRespose(response.body());
                }else {
                    mastercommentListner.onFail("FAIL");
                }
            }

            @Override
            public void onFailure(Call<List<MasterCommentBean>> call, Throwable t) {
                mastercommentListner.onFail(t.toString());
            }
        });
    }
}
